package cn.good.yan.sf.t7;

/**
 * 7. 有效的括号  -  判断是否是有效的括号
 *
 * @author shengren.yan
 * @create 2024-07-27
 */
public class ValidParentheses {


    public static boolean isValid(String str) {
        ArrayStack<String> stack = new ArrayStack<>(str.length());
        for (int i = 0; i < str.length(); i++) {
            String c = String.valueOf(str.charAt(i));
            if (c.equals("(")) {
                stack.push(")");
            } else if (c.equals("[")) {
                stack.push("]");
            } else if (c.equals("{")) {
                stack.push("}");
            } else {
                if (!stack.isEmpty() && stack.peek().equals(c)) {
                    stack.pol();
                } else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {

        // 判断是否是有效的括号
        System.out.println(isValid("([{}])"));
        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("()"));
        System.out.println("---------------------------");

        System.out.println(isValid("[)"));
        System.out.println(isValid("([)]"));
        System.out.println(isValid("([]"));
        System.out.println(isValid("("));

        System.out.println("---------------------------");
        System.out.println(isValid(")("));
        System.out.println(isValid("]"));

    }

}
